package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.common.vo.PageInfoVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.JazQuestionDO;
import com.jinzhi.jzweb.service.JazQuestionService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 常见问题
 * </pre>
 * <small> 2024-07-24 16:26:40 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazQuestion")
public class JazQuestionController extends BaseController {
	@Autowired
	private JazQuestionService jazQuestionService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:jazQuestion:jazQuestion")
	String JazQuestion(){
	    return "jzweb/jazQuestion/jazQuestion";
	}
	
	@ResponseBody
	@GetMapping("/list")
	//@RequiresPermissions("jzweb:jazQuestion:jazQuestion")
	public Result<Page<JazQuestionDO>> list(JazQuestionDO jazQuestionDTO, PageInfoVo pageInfoVo){
		Page<JazQuestionDO> questionDOPage = new Page<>(pageInfoVo.getCurrent(), pageInfoVo.getSize());
		// 查询列表数据
		Page<JazQuestionDO> page = jazQuestionService.selectPage(questionDOPage,
				new EntityWrapper<JazQuestionDO>()
						.like(StringUtil.notEmpty(jazQuestionDTO.getTitle()),"title",jazQuestionDTO.getTitle())
						.eq(jazQuestionDTO.getType()!=null,"type",jazQuestionDTO.getType())
						.orderBy("-order_limit",false)
						.orderBy("create_time",false));
        return Result.ok(page);
	}


	@ResponseBody
	@GetMapping("/userOrAuntList")  //用户端或小程序调用
	public Result<Page<JazQuestionDO>> userOrAuntList(JazQuestionDO jazQuestionDTO, PageInfoVo pageInfoVo){
		Page<JazQuestionDO> questionDOPage = new Page<>(pageInfoVo.getCurrent(), pageInfoVo.getSize());
		// 查询列表数据
		Page<JazQuestionDO> page = jazQuestionService.selectPage(questionDOPage,
				new EntityWrapper<JazQuestionDO>()
						.like(StringUtil.notEmpty(jazQuestionDTO.getTitle()),"title",jazQuestionDTO.getTitle())
						.eq(jazQuestionDTO.getType()!=null,"type",jazQuestionDTO.getType())
						.eq("status",1)
						.orderBy("order_limit",true)
						.orderBy("create_time",false));
		return Result.ok(page);
	}



	@ResponseBody
	@GetMapping("/questionDetails")
	//@RequiresPermissions("jzweb:jazQuestion:jazQuestion")
	public Result<JazQuestionDO> questionDetails(Long id){
		JazQuestionDO jazQuestionDO = jazQuestionService.selectById(id);
		return Result.ok(jazQuestionDO);
	}

	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:jazQuestion:add")
	String add(){
	    return "jzweb/jazQuestion/add";
	}

	@GetMapping("/edit/{id}")
	//@RequiresPermissions("jzweb:jazQuestion:edit")
	String edit(@PathVariable("id") Long id,Model model){
		JazQuestionDO jazQuestion = jazQuestionService.selectById(id);
		model.addAttribute("jazQuestion", jazQuestion);
	    return "jzweb/jazQuestion/edit";
	}
	
	@Log(value = "添加常见问题",sysModule = 13,operationType = 1)
	@ResponseBody
	@PostMapping("/save")
	//@RequiresPermissions("jzweb:jazQuestion:add")
	public Result<String> save(@RequestBody JazQuestionDO jazQuestion){
		if (jazQuestion==null){
			return Result.fail();
		}
		boolean insert = jazQuestionService.insert(jazQuestion);
		return insert?Result.ok():Result.fail();
	}

	@Log(value = "修改常见问题状态",sysModule = 13,operationType = 3)
	@ResponseBody
	@PostMapping("/saveStatus" )
	public Result<String> saveStatus(@RequestBody JazQuestionDO jazQuestion){
		if (jazQuestion==null){
			return Result.fail();
		}
		JazQuestionDO jazQuestionDO = jazQuestionService.selectById(jazQuestion.getId());
		if (jazQuestionDO==null){
			return Result.fail();
		}
		jazQuestionDO.setStatus(jazQuestion.getStatus());
		boolean insert = jazQuestionService.updateById(jazQuestionDO);
		return insert?Result.ok():Result.fail();
	}
	
	@Log(value = "修改常见问题",sysModule = 13,operationType = 3)
	@ResponseBody
	@PostMapping("/update")
	//@RequiresPermissions("jzweb:jazQuestion:edit")
	public Result<String>  update(@RequestBody JazQuestionDO jazQuestion){
		if (jazQuestion==null){
			return Result.fail();
		}
		boolean update = jazQuestionService.updateById(jazQuestion);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log(value = "删除常见问题",sysModule = 13,operationType = 2)
	@PostMapping( "/remove")
	@ResponseBody
	//@RequiresPermissions("jzweb:jazQuestion:remove")
	public Result<String>  remove( Long id){
		JazQuestionDO jazQuestionDO = jazQuestionService.selectById(id);
		if (jazQuestionDO == null){
			return Result.fail();
		}
		if (jazQuestionDO.getStatus()==1){
			return Result.fail("请将问题关闭后在删除！");
		}
		boolean b = jazQuestionService.deleteById(id);
		return b?Result.ok():Result.fail();
	}

	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:jazQuestion:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		jazQuestionService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
